package com.yc.network;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLDemo {

    public static void main(String[] args) throws IOException {

        // URL  格式:  协议://IP(域名):端口/资源路径/文件名?参数列表#锚点
        URL url = new URL("https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.25/mysql-connector-java-8.0.25.jar");

        System.out.println(url.getProtocol());// 获取协议  http
        System.out.println(url.getPort()); // 端口
        System.out.println(url.getFile());// 文件名

        URLConnection conn = url.openConnection();
        InputStream inputStream = conn.getInputStream();

        FileOutputStream fileOutputStream = new FileOutputStream("d:/mysql-connector-java-8.0.25.jar");
        byte[] buffer = new byte[1024];
        int count;
        while( ( count = inputStream.read(buffer)) > -1  ){
            fileOutputStream.write(buffer,0,count);
        }
        inputStream.close();

        // JSoup Java网页解析, 制作网络爬虫

    }
}
